# cython: language_level=3
import app.model_phl.my_tool_package as my_package
import utils
import app.utils.util_log as util_log
import datetime

def get_is_rule_featues(freatures,key):
    if 'none' == freatures.get(key, 'none') or freatures.get(key, 'none') is None:
        util_log.get_logger().warning('get_is_rule_featues {} error'.format(key))
        return False
    else:
        return True

class RepayNewRules(object):
    """
    他盘老客规则
    """
    def __int__(self):
        pass

    def initial_abtest(self,free_features):
        """
        abtest分流
        :param free_features:
        :return:
        """
        pass

    def rules(self, free_features):
        """
        他盘老客规则
        :param free_features:
        :return:
        """
        rule_sets_result = {}
        rule_result = self.__ordinary_rule__(free_features=free_features)
        rule_sets_result.update(rule_result)
        score_result = self.__score_rule__(free_features=free_features)
        rule_sets_result.update(score_result)
        return rule_sets_result

    def preloan_amount(self,free_features,FraudAssessmentResults):
        """
        他盘老客额度
        :param free_features:
        :param FraudAssessmentResults:
        :return:
        """
        return self.__preloan_amount__(free_features=free_features,FraudAssessmentResults=FraudAssessmentResults)

    def __preloan_amount__(self,free_features,FraudAssessmentResults):
        """
        额度策略
        :param free_features:
        :param FraudAssessmentResults:
        :return:
        """
        amount = 0

        if FraudAssessmentResults == 'reject':
            return amount
        amount = 1500
        return amount

    def __score_rule__(self,free_features):
        """
        信用分规则
        :param free_features:
        :return:
        """
        rule_sets_result = {}

        creditScoreV7 = free_features.get('creditScoreV7',0)

        #信用评分规则,针对不同的包设置不同的阈值
        if creditScoreV7 <= 999:
            rule_sets_result['m501'] = 1

        return rule_sets_result

    def __ordinary_rule__(self,free_features):
        """
        他盘老客策略
        编码原则：
        1 开头： 准入规则
        2 开头： 欺诈规则
        3 开头： 黑名单规则
        4 开头： 信用规则
        5 开头：信用评分规则
        6 开头 临时规则
        :param free_features:
        :return:
        """
        rule_sets_result = {}
        # 准入规则
        if get_is_rule_featues(free_features, 'age') and ((free_features['age'] > 0 and free_features['age'] < 18) or free_features[
            'age'] > 65):
            rule_sets_result['m101'] = 1
        if get_is_rule_featues(free_features, 'userHaveNoRepayOrderCnt') and free_features['userHaveNoRepayOrderCnt'] > 0:
            rule_sets_result['m102'] = 1

        # 欺诈规则
        if get_is_rule_featues(free_features, 'userinfoNameSameWithEmerContact') and free_features[
            'userinfoNameSameWithEmerContact'] == 1:
            rule_sets_result['m201'] = 1
        if get_is_rule_featues(free_features, 'userinfoPhoneSamewithEmerContact') and free_features[
            'userinfoPhoneSamewithEmerContact'] == 1:
            rule_sets_result['m202'] = 1
        if get_is_rule_featues(free_features, 'emailRelateApplyUserCnt30D') and free_features[
            'emailRelateApplyUserCnt30D'] > 1:
            rule_sets_result['m203'] = 1
        if get_is_rule_featues(free_features, 'homeAddressRelateApplyUserCnt3D') and free_features[
            'homeAddressRelateApplyUserCnt3D'] > 3:
            rule_sets_result['m204'] = 1
        if get_is_rule_featues(free_features, 'nameRelateApplyUserCnt30D') and free_features[
            'nameRelateApplyUserCnt30D'] > 2:
            rule_sets_result['m205'] = 1
        if get_is_rule_featues(free_features, 'nameBirthDayRelateApplyUserCnt30D') and free_features[
            'nameBirthDayRelateApplyUserCnt30D'] > 2:
            rule_sets_result['m206'] = 1
        if get_is_rule_featues(free_features, 'emerConcatPhoneApplyUserUsedCnt60D') and free_features[
            'emerConcatPhoneApplyUserUsedCnt60D'] > 2:
            rule_sets_result['m207'] = 1
        if get_is_rule_featues(free_features, 'emerConcatPhoneIsApplyUsersCnt60D') and free_features[
            'emerConcatPhoneIsApplyUsersCnt60D'] > 1:
            rule_sets_result['m208'] = 1
        if get_is_rule_featues(free_features, 'phoneRelateApplyUserCnt30D') and free_features[
            'phoneRelateApplyUserCnt30D'] > 1:
            rule_sets_result['m209'] = 1
        if get_is_rule_featues(free_features, 'phoneRelateApplyUserNameCnt30D') and free_features[
            'phoneRelateApplyUserNameCnt30D'] > 2:
            rule_sets_result['m210'] = 1
        if get_is_rule_featues(free_features, 'deviceWifimacRelateApplyUserCnt30D') and free_features[
            'deviceWifimacRelateApplyUserCnt30D'] > 3:
            rule_sets_result['m211'] = 1
        if get_is_rule_featues(free_features, 'deviceMacRelateApplyUserCnt30D') and free_features[
            'deviceMacRelateApplyUserCnt30D'] > 1:
            rule_sets_result['m232'] = 1
        if get_is_rule_featues(free_features, 'deviceReleaseDateAudioExternalRelateApplyUserCnt1D') and free_features[
            'deviceReleaseDateAudioExternalRelateApplyUserCnt1D'] > 1:
            rule_sets_result['m233'] = 1
        if get_is_rule_featues(free_features, 'deviceReleaseDateImagesExternalRelateApplyUserCnt1D') and free_features[
            'deviceReleaseDateImagesExternalRelateApplyUserCnt1D'] > 1:
            rule_sets_result['m234'] = 1
        if get_is_rule_featues(free_features, 'deviceReleaseDateVideoExternalRelateApplyUserCnt1D') and free_features[
            'deviceReleaseDateVideoExternalRelateApplyUserCnt1D'] > 1:
            rule_sets_result['m235'] = 1
        if get_is_rule_featues(free_features, 'deviceWifinameRelateApplyUserCnt30D') and free_features[
            'deviceWifinameRelateApplyUserCnt30D'] > 3:
            rule_sets_result['m212'] = 1
        if get_is_rule_featues(free_features, 'deviceImeiRelateApplyUserCnt30D') and free_features[
            'deviceImeiRelateApplyUserCnt30D'] > 1:
            rule_sets_result['m213'] = 1
        if get_is_rule_featues(free_features, 'deviceLocationRelateApplyUserCnt30D') and free_features[
            'deviceLocationRelateApplyUserCnt30D'] > 3:
            rule_sets_result['m214'] = 1
        if get_is_rule_featues(free_features, 'deviceLocationInterval200mileApplyUserCnt120Minutes') and free_features[
            'deviceLocationInterval200mileApplyUserCnt120Minutes'] > 1:
            rule_sets_result['m215'] = 1
        if get_is_rule_featues(free_features, 'userOCRIDNoRelateApplyUserCnt60D') and free_features[
            'userOCRIDNoRelateApplyUserCnt60D'] > 1:
            rule_sets_result['m220'] = 1
        if get_is_rule_featues(free_features, 'userOCRIDNoRelateUserNameCnt60D') and free_features[
            'userOCRIDNoRelateUserNameCnt60D'] > 1:
            rule_sets_result['m221'] = 1
        if get_is_rule_featues(free_features, 'userOCRIDNoRelatePhoneCnt60D') and free_features[
            'userOCRIDNoRelatePhoneCnt60D'] > 1:
            rule_sets_result['m222'] = 1
        if get_is_rule_featues(free_features, 'userBankCardRelateApplyUserCnt60D') and free_features[
            'userBankCardRelateApplyUserCnt60D'] > 1:
            rule_sets_result['m223'] = 1
        if get_is_rule_featues(free_features, 'userBankCardRelateUserIDNoCnt60D') and free_features[
            'userBankCardRelateUserIDNoCnt60D'] > 1:
            rule_sets_result['m224'] = 1
        if get_is_rule_featues(free_features, 'userBankCardRelatePhoneCnt60D') and free_features[
            'userBankCardRelatePhoneCnt60D'] > 1:
            rule_sets_result['m225'] = 1
        if get_is_rule_featues(free_features, 'userNameOCRNameCompareSimilarity') and (
                free_features['userNameOCRNameCompareSimilarity'] <= 0.5 and free_features['userNameOCRNameCompareSimilarity'] >= 0):
            rule_sets_result['m226'] = 1
        if get_is_rule_featues(free_features, 'userIDNumberOCRIDNumberCompareSimilarity') and (
                free_features['userIDNumberOCRIDNumberCompareSimilarity'] <= 0.5):
            rule_sets_result['m227'] = 1
        if get_is_rule_featues(free_features, 'ocrCardIsNull') and (free_features['ocrCardIsNull'] == 1):
            rule_sets_result['m228'] = 1
        # if get_is_rule_featues(free_features, 'userBankCardIsHaveRepayOrderCnt') and (free_features['userBankCardIsHaveRepayOrderCnt'] == 0):
        #     rule_sets_result['m231'] = 1
        # 黑名单规则
        if get_is_rule_featues(free_features, 'userPhoneInBlackLevel') and free_features['userPhoneInBlackLevel'] >= 1:
            rule_sets_result['m301'] = 1
        if get_is_rule_featues(free_features, 'userIDCradInBlackLevel') and free_features[
            'userIDCradInBlackLevel'] >= 1:
            rule_sets_result['m302'] = 1
        if get_is_rule_featues(free_features, 'userOCRIDCradInBlackLevel') and free_features[
            'userOCRIDCradInBlackLevel'] >= 1:
            rule_sets_result['m303'] = 1
        if get_is_rule_featues(free_features, 'userImeiInBlackLevel') and free_features['userImeiInBlackLevel'] >= 1:
            rule_sets_result['m304'] = 1
        if get_is_rule_featues(free_features, 'userEmailInBlackLevel') and free_features['userEmailInBlackLevel'] >= 1:
            rule_sets_result['m305'] = 1
        if get_is_rule_featues(free_features, 'userWifiMacInBlackLevel') and free_features[
            'userWifiMacInBlackLevel'] >= 1:
            rule_sets_result['m306'] = 1
        if get_is_rule_featues(free_features, 'userBankCardInBlackLevel') and free_features[
            'userBankCardInBlackLevel'] >= 1:
            rule_sets_result['m307'] = 1
        if get_is_rule_featues(free_features, 'userPhoneIsInBlack') and free_features['userPhoneIsInBlack'] == 1:
            rule_sets_result['m308'] = 1
        #多头规则
        if get_is_rule_featues(free_features, 'userPhoneHaveOrder') and free_features['userPhoneHaveOrder'] > 20:
            rule_sets_result['m401'] = 1
        if get_is_rule_featues(free_features, 'userOCRIDCradHaveOrder') and free_features['userOCRIDCradHaveOrder'] > 20:
            rule_sets_result['m402'] = 1
        if get_is_rule_featues(free_features, 'userIDCradHaveOrder') and free_features['userIDCradHaveOrder'] > 20:
            rule_sets_result['m403'] = 1

        #他盘老客阈值拒绝配置
        if get_is_rule_featues(free_features, 'userMultiIsOnOverdueAllApplyApplyIntervalOrderCntAllD') and free_features['userMultiIsOnOverdueAllApplyApplyIntervalOrderCntAllD'] >= 1 and free_features['creditScoreV6']<530:
            rule_sets_result['m404'] = 1

        if free_features.get('userPackageId', '') == 'micro.loan.app':
            rule_sets_result['m601'] = 1

        return rule_sets_result
